#ifndef _LT_MAP_STATE_H_
#define _LT_MAP_STATE_H_

#include <list>
#include "ltGameState.h"
#include "fastsig/fastsig.hpp"
#include "OgreODE/OgreODECollision.h"

namespace Lotus
{
	class MapObject;

	namespace mapformat
	{
		class MapObject;
	}

	class MapObjectIterator
	{
	public:
		virtual bool iterate(MapObject* object)=0;
		virtual ~MapObjectIterator(){};
	};

	class Collider
	{
	public:
		virtual void onCollision(OgreOde::Contact* contact) = 0;
		unsigned int listenFlag;
		unsigned int collisionFlag;
	};

	class LOTUS_API MapState:public IGameState, public OgreOde::CollisionListener
	{
	public:
		MapState();
		virtual ~MapState();
		
		MapObject* createObject(const char* className,MapObject* parent=0);
		MapObject* getObject(const char* name);
		void destroyObject(MapObject* object);
		void iterateObjects(MapObjectIterator* iterator);
		void setActive(MapObject* object, bool active);
		bool isActive(MapObject* object) const;
		void setUpdateInterval(Ogre::Real interval);
		void setDeleteOnLeave(bool value);
		
		virtual void onCreate();
		virtual void onEnter();
		virtual void onLeave();
		virtual void update(Ogre::Real timeSinceLastFrame);

		fastsig::signal<void(Ogre::Real)> FrameEvent;

		OgreOde::World* getPhysicWorld() const;

		void saveObject(MapObject* object,mapformat::MapObject* objectData) const;
		MapObject* loadObject(mapformat::MapObject* object,MapObject* parent);

	private:
		void addObject(MapObject* object);
		void removeObject(MapObject* object);

		bool collision(OgreOde::Contact* contact);

		bool deleteOnLeave;
		OgreOde::World* physicWorld;
		OgreOde::StepHandler* mStepper;
		Ogre::Real updateTimer,interval;
		std::list<MapObject*> objects;
		std::map<std::string,MapObject*> objectWidthNames;
		fastsig::signal<void()> updateSignal;
	};
}

#endif